package com.api.volunteer.service.impl;

import com.api.volunteer.model.MessageRecord;
import com.api.volunteer.model.VolunteerActivities;
import com.api.volunteer.model.VolunteerRecord;
import com.api.volunteer.repository.MessageRecordRepository;
import com.api.volunteer.repository.VolunteerActivitesRepository;
import com.api.volunteer.repository.VolunteerRecordRepository;
import com.api.volunteer.service.VolunteerActivitesService;
import com.api.volunteer.util.Holder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 邹宇杰
 * @email 2901570623@qq.com
 * @date 2022/4/1 12:08
 */
@Service("volunteerActivitesService")
@Transactional(rollbackOn = Exception.class)
public class VolunteerActivitesServiceImpl implements VolunteerActivitesService {

    @Autowired
    VolunteerActivitesRepository volunteerActivitesRepository;

    @Autowired
    VolunteerRecordRepository volunteerRecordRepository;

    @Autowired
    Holder holder;

    @Autowired
    MessageRecordRepository messageRecordRepository;

    @Override
    public void save(VolunteerActivities volunteerActivities) {
        volunteerActivities.setOpenid(holder.getUser().getOpenid());
        volunteerActivitesRepository.save(volunteerActivities);
    }

    @Override
    public void joinVolunteer(String openid, String volunteerId) {
        VolunteerActivities volunteerActivities = volunteerActivitesRepository.findVolunteerActivitiesById(volunteerId);
        VolunteerRecord volunteerRecord = new VolunteerRecord();
        volunteerRecord.setOpenid(openid);
        volunteerRecord.setVolunteerId(volunteerId);
        volunteerRecord.setBeginDate(volunteerActivities.getBeginDate());
        volunteerRecord.setEndDate(volunteerActivities.getEndDate());
        volunteerRecord.setAddress(volunteerActivities.getAddress());
        volunteerRecordRepository.save(volunteerRecord);

    }

    @Override
    public List<VolunteerActivities> list() {
        return volunteerActivitesRepository.findAll();
    }

    @Override
    public VolunteerActivities getVolunteerActivities(String volunteerId) {
        return volunteerActivitesRepository.findVolunteerActivitiesById(volunteerId);
    }

    @Override
    public List<MessageRecord> listMessage(String volunteerId, String openid, String toOpenid) {

        List<MessageRecord> messageRecordBySessionId = messageRecordRepository.findMessageRecordBySessionIdOrderByCreateDate(openid + toOpenid);

        List<MessageRecord> messageRecordBySessionId1 = messageRecordRepository.findMessageRecordBySessionIdOrderByCreateDate(toOpenid + openid);

        if (messageRecordBySessionId.isEmpty()){
            if(messageRecordBySessionId1.isEmpty()){
                return new ArrayList<>();
            }else{
                return messageRecordBySessionId1;
            }
        }else{
            return messageRecordBySessionId;
        }

    }

    @Override
    public void saveMessageRecord(String volunteerId, String openid, String toOpenid, String message) {

        List<MessageRecord> messageRecordBySessionId = messageRecordRepository.findMessageRecordBySessionIdOrderByCreateDate(openid + toOpenid);

        List<MessageRecord> messageRecordBySessionId1 = messageRecordRepository.findMessageRecordBySessionIdOrderByCreateDate(toOpenid + openid);

        if (messageRecordBySessionId.isEmpty()){
            if(messageRecordBySessionId1.isEmpty()){
                MessageRecord messageRecord = new MessageRecord();
                messageRecord.setOpenid(openid);
                messageRecord.setToOpenid(toOpenid);
                messageRecord.setMsg(message);
                messageRecord.setVolunteerId(volunteerId);
                messageRecord.setSessionId(openid+toOpenid);
                messageRecordRepository.save(messageRecord);
            }else{
                MessageRecord messageRecord = new MessageRecord();
                messageRecord.setOpenid(openid);
                messageRecord.setToOpenid(toOpenid);
                messageRecord.setMsg(message);
                messageRecord.setVolunteerId(volunteerId);
                messageRecord.setSessionId(toOpenid+openid);
                messageRecordRepository.save(messageRecord);
            }
        }else{
            MessageRecord messageRecord = new MessageRecord();
            messageRecord.setOpenid(openid);
            messageRecord.setToOpenid(toOpenid);
            messageRecord.setMsg(message);
            messageRecord.setVolunteerId(volunteerId);
            messageRecord.setSessionId(openid+toOpenid);
            messageRecordRepository.save(messageRecord);
        }

    }
}
